﻿//-----------------------------------------------------------------------
// <copyright file="TemplateGroup.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Business;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.ManagementEngine;
using FastNET.CleanAct.Common.Repository.Interfaces.Factories;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.ManagementEngine;

#endregion

namespace FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects
{
    /// <summary>
    /// Business Class representing a Template Group for the client
    /// </summary>
    public class TemplateGroup : BusinessBase<ITemplateGroupData, ITemplateGroupRepository>
    {
        #region AutoLoaded Property Fields



        #endregion

        #region CTor/DTor

        #region Creation Routines

        /// <summary>
        /// Initializes a new instance of the TemplateGroup class
        /// </summary>
        public TemplateGroup()
            : this(new RepositoryFactory())
        {
        }

        /// <summary>
        /// Initializes a new instance of the TemplateGroup class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        public TemplateGroup(IRepositoryFactory factory)
            : base(factory)
        {
        }

        #endregion

        #region Retrieve by IDs

        /// <summary>
        /// Initializes a new instance of the TemplateGroup class
        /// </summary>
        /// <param name="parentID">Identity of the parent template part of the map to retrieve</param>
        /// <param name="childID">Identity of the child template part of the map to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if characterID or classID are null</exception>
        public TemplateGroup(Identity parentID, Identity childID)
            : this(new RepositoryFactory(), parentID, childID)
        {
        }

        /// <summary>
        /// Initializes a new instance of the CharacterClass class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="parentID">Identity of the parent template part of the map to retrieve</param>
        /// <param name="childID">Identity of the child template part of the map to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        public TemplateGroup(IRepositoryFactory factory, Identity parentID, Identity childID)
            : base(factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Cannot retrieve data without a factory");
            }

            Repository = RetrieveRepository(factory);
            Data = Repository.Retrieve(parentID, childID);
            Factory = factory;

            if (Data == null)
            {
                throw new InvalidOperationException(string.Format("Unable to retrieve data from database with parentID of {0} and childID of {1}", parentID, childID));
            }
        }

        #endregion

        #region Retrieve by Data

        /// <summary>
        /// Initializes a new instance of the TemplateGroup class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">ITemplateGroupData of the data to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or ID are null</exception>
        internal TemplateGroup(IRepositoryFactory factory, ITemplateGroupData data)
            : base(factory, data)
        {
        }

        #endregion

        #endregion

        #region Overrides of BusinessBase<ITemplateGroupData,ITemplateGroupRepository>

        /// <summary>
        /// Method to check whether or not the current object is valid
        /// </summary>
        /// <returns>Boolean based on the validity of the object</returns>
        public override bool IsValid()
        {
            return true;
        }

        #endregion

        #region Data Properties

        /// <summary>
        /// Gets or sets the Parent Template Identity associated with this mapping
        /// </summary>
        public Identity ParentTemplateID
        {
            get
            {
                return Data.ParentTemplateID;
            }

            set
            {
                Data.ParentTemplateID = value;
            }
        }

        /// <summary>
        /// Gets or sets the Child Template Identity associated with this mapping
        /// </summary>
        public Identity ChildTemplateID
        {
            get
            {
                return Data.ChildTemplateID;
            }

            set
            {
                Data.ChildTemplateID = value;
            }
        }

        #endregion

        #region Public Static Factory Methods

        /// <summary>
        /// Retrieves a list of template groups
        /// </summary>
        /// <returns>IList of TemplateGroup found or null if no items were found</returns>
        public static IList<TemplateGroup> Retrieve()
        {
            return Retrieve(new RepositoryFactory());
        }

        /// <summary>
        /// Retrieves a list of template groups for the specified primary ID
        /// </summary>
        /// <param name="primaryID">The primary ID to find</param>
        /// <returns>IList of TemplateGroup found or null if no items were found</returns>
        public static IList<TemplateGroup> Retrieve(Identity primaryID)
        {
            return Retrieve(new RepositoryFactory(), primaryID);
        }

        #endregion

        #region Internal Static Factory Methods

        #region Retrieve

        /// <summary>
        /// Retrieves a list of template groups
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>IList of TemplateGroup found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<TemplateGroup> Retrieve(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<ITemplateGroupData> items = RetrieveRepository(factory).Retrieve();
            IList<TemplateGroup> returnItems = new List<TemplateGroup>();

            foreach (ITemplateGroupData item in items)
                returnItems.Add(new TemplateGroup(factory, item));

            return returnItems;
        }

        /// <summary>
        /// Retrieves a list of template groups for the specified primary ID
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <param name="primaryID">The primary ID to find</param>
        /// <returns>IList of TemplateGroup found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<TemplateGroup> Retrieve(IRepositoryFactory factory, Identity primaryID)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<ITemplateGroupData> items = RetrieveRepository(factory).Retrieve(primaryID);
            IList<TemplateGroup> returnItems = new List<TemplateGroup>();

            foreach (ITemplateGroupData item in items)
                returnItems.Add(new TemplateGroup(factory, item));

            return returnItems;
        }

        #endregion

        #endregion
    }
}